<?php
/**
 * Created by PhpStorm.
 * User: moquan
 * Date: 2019/3/12
 * Time: 20:39
 */

namespace app\admin\model;

class AdminRole extends BaseModel {
    protected $append  = ['id', 'text'];

    protected function getIdAttr($value, $data){
        return $data['role_id'];
    }

    protected function getTextAttr($value, $data){
        return $data['role_name'];
    }

    // 写入时，将菜单id转成json格式
    public function setMenuAuthAttr($value){
        return empty($value) ? '' : json_encode($value);
    }

    // 读取时，将菜单id转为数组
    public function getMenuAuthAttr($value){
        return json_decode($value, true);
    }

    protected function getStatusNameAttr($value, $data){
        $name = '';

        if ( $value == 1 ) {
            $name = '开启';
        }
        if ( $value == 0 ) {
            $name = '禁用';
        }

        return $name;
    }

    static function getRoleMenuAuth( $menu_id ) {
        return self::where('menu_auth', 'like', '%"'.$menu_id.'"%')->select();
    }

    function authority(){
        return $this->hasMany('AdminAuthority', 'role_id', 'role_id');
    }

    public static function getRoleByID( $id ) {
        return self::where( 'role_id', $id )->find();
    }

    public static function getAdminRoleList() {
        $list = self::order('role_id desc')->paginate();
        return $list;
    }

    public static function getAdminRoleByMenuId() {
//        $list = self::field("*,if(LOCATE('".$menuId."','menu_auth')>0,'true','false') as selected")->where('role_id', '>', 1)->order('role_id desc')->paginate();
        $list = self::where('role_id', '>', 1)->order('role_id desc')->paginate();
        return $list;
    }

    public static function delRoleByID( $id ) {
        return self::where( 'role_id', $id )->delete();
    }

}